Link to this headingBlake3
- Uses Merkle Tree to hash input
- Input chunked into 128 bytes
- In the Chacha Round step the input_data is permuted rather than the internal chacha buffer
Link to this headingImplementation
"""docstring for Blake3Node"""
#Flags:
# CHUNK_START = 0x01
# CHUNK_END = 0x02
# PARENT = 0x04
# ROOT = 0x08
# KEYED_HASH = 0x10
# DERIVE_KEY_CONTEXT = 0x20
# DERIVE_KEY_MATERIAL = 0x40
=
=
= b
=
=
= 1024
= 64
#Compression Settings
=
=
= 7
=
= 32
#
=
return * +
#Calculate indexes from Permuation table and round_index and offset
#Calculate indexes from Permutation table and round_index and offset
=
#Modified first part to include message and round xor
=
=
=
=
=
=
#Modified first part to include message and round xor
=
=
=
=
=
=
return
=
#Use the permutation lookup table to get new index
=
=
return
#Extend inputdata
=
=
#Check input length
assert == 16
'''
|chainedValue |chainedValue |chainedValue |chainedValue |
|chainedValue |chainedValue |chainedValue |chainedValue |
|IV |IV |IV |IV |
|blockcounter[0] |blockcounter[0] |blocklen |flags |
'''
#Start setting up the temp buffers
= + +
#Add the Number of blocks that have been processed
^=
^=
#Add the number of bytes in the current block to be hashed
=
=
#print(f"compress: {chaining_values[0]}, {counter}, {flags}, {block_length}, {input_data}")
#print(f"before: {[hex(x) for x in temp_buffers]}")
#Do ChaCha rounds with modifications
#Do Each Column
, , , =
, , , =
, , , =
, , , =
#Do Each Diagonal
, , , =
, , , =
, , , =
, , , =
#Black3 only permuste the input data
#Blake3 only permutes the input data
=
#print(f"after: {[hex(x) for x in temp_buffers]}")
#Update Buffers
^=
^=
#print(f"done: {[hex(x) for x in temp_buffers]}")
return
#Set defaults
=
=
=
=
=
#Add the flags to the end
#Set CHUNK_START flag
|= 0x01
#Set CHUNK_END
|= 0x02
#Overwride defaults if needed
#Override defaults if needed
=
=
=
=
|=
return
#Check if block is currently full
=
#Update Compressed
+=1
= b
#Add up to the max_block_length
=
+=
=
#If less than 64 bytes pad data
=
#Add the END_CHUNK Flag
return
#Blake3 Constants
#Chunk State Varables
=
= 32
=
=
#Blake3 with a custom IV For keyed hashing
#If specifying a key it must be 32 bytes to fit into the buffers
assert == 32
=
= 0x10
#Since the key is set the flag for Keyed Hash (16 = 0x10)
=
#Blake3 to derive key from personalization
#Get the Blake3 Hash of rhe personaliztion message to use for the key.
#Get the Blake3 Hash of the personalization message to use for the key.
=
|= 0x20
#Set the key to the derived key
=
#Set the DERIVE_KEY_MATERIAL flag (64 = 0x40)
= 0x40
=
= 0x00
=
#Convert to bytes if not already
=
#Set Final Length
=
#Pad the data to a multable of the block size
#Pad the data to a multiple of the block size
return
#Check If new chunk is the first one in the next level
#Get the Left Node
=
#Compress the left and right node with the parent flag
=
#Move Chunk to the next level to compress
>>= 1
#Add Data to Chunks
#Test if chunk reaches max_size then add a new chunk node
#Get Chaining Value
=
#Update and Reset Data
+= 1
= b
#Update Chunk and Check if needs to compress
#Create New Chunk
=
#Add data to chunk up to the chunk_length
=
#Send Buffer to the chunk
#Remove the Data that was sent to the chunk
=
=
=
=
#Set the End Flag for the next compress
|= 0x02
##Compress all Parent Values to a single Value
#Decrease Stack Number
-= 1
#Set the Parent Flag globaly until the end
#Set the Parent Flag globally until the end
#Get Current Chaining Value
#If is the first time get the output
=
=
#Setup the next Chain
=
=
#Do Final Compress from the root
= 0
=
#Set the ROOT Flag
+=
+=
+= 1
return
return
#messages = [b"TESTDATA", b"TESTDATA" *10, b"TESTDATA" * 200] #b"TESTDATA" * 1000
=
#blake3 = Blake3(key=b"\xBB\x67\xAE\x85"*8)
#blake3 = Blake3(personalization=b"pure_blake3 2021-10-29 18:37:44 example context")
=
#print(f"blake3.finalize()")
=